package com.game.modular.game.playerMatch.service;

import com.game.modular.game.playerMatch.service.base.PlayerMatchBaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import com.game.modular.game.playerInfo.mapper.PlayerMatchMapper;
import com.game.modular.game.playerInfo.domain.PlayerMatch;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 玩家对局信息(PlayerMatch)表服务实现类
 *
 * @author cxy
 * @since 2024-08-19 16:41:20
 */
@Slf4j
@Service
public class PlayerMatchServiceImpl extends PlayerMatchBaseServiceImpl {
    @Resource
    private PlayerMatchMapper playerMatchMapper;

    public void init(Long playerId) {
        PlayerMatch build = PlayerMatch.builder()
                .uid(playerId)
                .currentMatchId(0L)
                .build();
        save(build);
    }


    // 获取当前对局id
    public Long currentMatchId(Long playerId){
        PlayerMatch playerMatch = playerMatchMapper.selectById(playerId);
        if (playerMatch == null){
            return 0L;
        }
        return playerMatch.getCurrentMatchId();
    }


    /**
     * 重置玩家 pvp 匹配信息
     */
    public void resetPvp(Long playerId) {
        PlayerMatch playerMatch = get(playerId);
        playerMatch.setCurrentMatchId(0L);
        updateById(playerMatch);
    }

    /**
     * 更新玩家当前对局id
     */
    public void updateMatch(
           @NotNull(message = "playerId不能为空") Long playerId,
           @NotNull(message = "pvpMatchId不能为空")  Long pvpMatchId
    ) {
        PlayerMatch playerMatch = get(playerId);
        if (playerMatch == null){
            init(playerId);
        }
        PlayerMatch build = PlayerMatch.builder()
                .uid(playerId)
                .currentMatchId(pvpMatchId)
                .build();
        updateById(build);
    }
}
